package com.xiao.quicknote;

import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class NoteListActivity extends ListActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_list);

		registerForContextMenu(getListView());
	}

	@Override
	protected void onStart() {
		super.onRestart();

		setListContent();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_note_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		boolean result = super.onOptionsItemSelected(item);

		switch (item.getItemId()) {
		case R.id.menu_search:
			SimpleAdapter adapter = (SimpleAdapter) getListAdapter();
			final Filter filter = adapter.getFilter();
			final EditText filterText = new EditText(this);
			new AlertDialog.Builder(this)
					.setView(filterText)
					.setPositiveButton(android.R.string.ok,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									String filterString = filterText.getText()
											.toString().trim();
									if (filterString.length() > 0) {
										filter.filter(filterString);
									}
								}
							}).setNegativeButton(android.R.string.cancel, null)
					.show();
			break;
		case R.id.menu_new:
			Intent newNoteIntent = new Intent(this, NoteEditActivity.class);
			startActivity(newNoteIntent);
			break;
		case R.id.menu_settings:
			Intent setPrefIntent = new Intent(this, PrefActivity.class);
			startActivity(setPrefIntent);
			break;
		default:
			break;
		}

		return result;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.activity_note_list_context, menu);

		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		int position = info.position;
		Map<String, String> noteInfo = getNoteInfo(position);
		String noteTitle = noteInfo.get("title");
		menu.setHeaderTitle(noteTitle);

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		boolean result = super.onContextItemSelected(item);

		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		int position = info.position;

		Map<String, String> noteInfo = getNoteInfo(position);
		final String notePath = noteInfo.get("path");

		switch (item.getItemId()) {
		case R.id.menu_delete:
			new AlertDialog.Builder(this)
					.setTitle(R.string.title_confirm_delete)
					.setPositiveButton(android.R.string.ok,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									NoteFileAdapter.deleteNote(notePath);
									setListContent();
								}
							}).setNegativeButton(android.R.string.cancel, null)
					.show();
			break;
		case R.id.menu_edit:
			Intent editNoteIntent = new Intent(this, NoteEditActivity.class);
			editNoteIntent.putExtra("path", notePath);
			startActivity(editNoteIntent);
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent viewNoteIntent = new Intent(this, NoteViewActivity.class);
		Map<String, String> noteInfo = getNoteInfo(position);

		String notePath = noteInfo.get("path");
		viewNoteIntent.putExtra("path", notePath);
		startActivity(viewNoteIntent);
	}

	private void setListContent() {
		NoteFileAdapter noteFileAdapter = new NoteFileAdapter();
		List<Map<String, String>> noteInfoList = noteFileAdapter
				.getNoteInfoList();
		ListAdapter adapter = new SimpleAdapter(this, noteInfoList,
				android.R.layout.simple_list_item_2, new String[] { "title",
						"first_line" }, new int[] { android.R.id.text1,
						android.R.id.text2 });
		setListAdapter(adapter);
	}

	private Map<String, String> getNoteInfo(int position) {
		@SuppressWarnings("unchecked")
		Map<String, String> noteInfo = (Map<String, String>) getListAdapter()
				.getItem(position);
		return noteInfo;
	}
}
